package com.liuhengjia.service.impl;

import com.liuhengjia.mapper.CategoryMapper;
import com.liuhengjia.pojo.Category;
import com.liuhengjia.service.CategoryService;
import com.liuhengjia.util.ThreadLocals;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void insertCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Map<String, Object> decodeMap = ThreadLocals.get();
        category.setCreateUser((Integer) decodeMap.get("id"));
        categoryMapper.insertCategory(category);
    }

    @Override
    public List<Category> selectAll() {
        Map<String, Object> map = ThreadLocals.get();
        Integer userId = (Integer) map.get("id");
        return categoryMapper.selectAll(userId);
    }

    @Override
    public Category selectById(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void deleteById(Integer id) {
        categoryMapper.delete(id);
    }

}
